﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.ReportSort
{
    public partial class GoodsList : BasePage
    {

        string SortField = "ProductName ASC";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {


                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
                {
                    BindList();
                }
                else
                {
                    Response.Write("<script>history.go(-1);<script>");
                }
            }
        }
        void BindList()
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            string strMosth = sq.Mid;
            sq.Mid = "";
            sq.Append = Function.FormatPlanTime("PlanTime", strMosth)  ;
            int t = 0;
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            ie.QueryObject = new HZ.Supervise.Model.QueryObject()
            {
                //分组字段
                GroupField = GroupByField.ProcureCatalogID,
                //查询条件
                Query = sq,
                //分组查询字段
                GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.PurchaseCountTotal | GroupFields.WareHouseRate,
                //排序字段
                SortField = SortField
            };

            //将数据填入gridview中
            System.Data.DataTable dt = ie.ExecutePage(1, 0, out t);
            sq.Mid = strMosth;
            gvwGoods.DataSource = dt;
            gvwGoods.DataBind();

        }

        protected void gvwGoods_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvwGoods_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                try
                {
                    query.ProcureCatalogID = e.CommandArgument.ToString();
                    query.ProductName = null;
                    query.HospitalName = null;
                    query.MedicineModelName = null;
                    query.SortSource = null;
                    query.Outlookc=null;
                    query.Unit=null;
                }
                catch { }
                Session[sessionid] = query;
                string url = "SortGoodsDetail.aspx";
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                sq.ProductName = this.txtProductName.Text.Trim();
                sq.HospitalName = this.txtHospitalName.Text.Trim();
                sq.MedicineModelName = this.txtMedicineModelName.Text.Trim();
                if (this.droSortSource.SelectedValue!="-1")
                {
                    sq.SortSource = int.Parse(this.droSortSource.SelectedValue);
                }
                sq.Outlookc = this.txtOutlookc.Text.Trim();
                sq.Unit = this.txtUnit.Text.Trim();
                SortField = this.droSortField.SelectedValue;
                AspNetPager1.CurrentPageIndex = 1;
            BindList();
        }
    }
}
